<?php
    // Connect to MySQL database
    require "config.php";
    
    $page = 1; // The current page
    $sortname = 'MaQuyCach'; // Sort column
    $sortorder = 'asc'; // Sort order
    $qtype = ''; // Search column
    $query = ''; // Search string
    
    // Get posted data
    if (isset($_POST['page'])) {
            $page = mysql_real_escape_string($_POST['page']);
    }
    if (isset($_POST['sortname'])) {
            $sortname = mysql_real_escape_string($_POST['sortname']);
    }
    if (isset($_POST['sortorder'])) {
            $sortorder = mysql_real_escape_string($_POST['sortorder']);
    }
    if (isset($_POST['qtype'])) {
            $qtype = mysql_real_escape_string($_POST['qtype']);
    }
    if (isset($_POST['query'])) {
            $query = mysql_real_escape_string($_POST['query']);
    }
    if (isset($_POST['rp'])) {
            $rp = mysql_real_escape_string($_POST['rp']);
    }
    else $rp = 5;
    
    // Setup sort and search SQL using posted data
    $sortSql = "order by $sortname $sortorder";
    $searchSql = ($qtype != '' && $query != '') ? "where $qtype = '$query'" : '';
    // Get total count of records
    $sql = "select count(*) from giaphucvn_quycach $searchSql";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    $total = $row[0];
    
    // Setup paging SQL
    $pageStart = ($page-1)*$rp;
    $limitSql = "limit $pageStart, $rp";
    
    $sql = "select * from giaphucvn_quycach $searchSql $sortSql $limitSql";
    //echo $sql;
    $results = mysql_query($sql);
    
    //Tao file xml
    header("Content-type: text/xml;");
    $stt = 1;
    $xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
    $xml .= "<rows>";
    $xml .= "<page>$page</page>";
    $xml .= "<total>$total</total>";
    while($fields = mysql_fetch_array($results))
    {
        $xml .= "<row id='".$fields['MaQuyCach']."'>";
        $xml .= "<cell><![CDATA[".$stt."]]></cell>";
    	$xml .= "<cell><![CDATA[".$fields['MaQuyCach']."]]></cell>";
    	$xml .= "<cell><![CDATA[".$fields['TenQuyCach']."]]></cell>";
        $xml .= "<cell><![CDATA["."<a href='index.php?action=capnhat_loaisanpham&sid=".$fields['MaQuyCach']."' onclick='editSpecifications({$fields['MaQuyCach']}); return false;'><img src='images/edit.png' alt='Sửa' /></a>"."]]></cell>";
        $xml .= "<cell><![CDATA["."<a href='index.php?action=xoa_loaisanpham&sid=".$fields['MaQuyCach']."' onclick='deleteSpecifications({$fields['MaQuyCach']}, \"{$fields['TenQuyCach']}\"); return false;'><img src='images/delete.png' alt='Xóa' /></a>"."]]></cell>";
    	$xml .= "</row>";
        $stt++;
    }
    $xml .= "</rows>";
    echo $xml;